/*
 * 
 * Copyright (C) 2006  Warren Konkel and Bounty Source, Inc.
 *  
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 */


function clickedFolder(id, url) {
  href = $('href' + id);
  tr = $('tr' + id);
  if (Element.hasClassName(href, "svnfolderopenlink")) {
    removeIdFromHash(id);

    Element.removeClassName(href, "svnfolderopenlink");
    Element.addClassName(href, "svnfolderlink");

    table = $('svnbrowsertable');
    children = table.getElementsByTagName('tr');
    removeList = [];

    for (i=0; i < children.length; i++) {
      if (children[i].id.indexOf(id + '_') != -1) {
        children[i].parentNode.removeChild(children[i]);
        i=i-1;
      }
    }

    updateAlternatingRows();
  } else if (Element.hasClassName(href, "svnfolderlink")) {
    addIdToHash(id);

    Element.removeClassName(href, "svnfolderlink");
    Element.addClassName(href, "svnspinnerlink");
    new Ajax.Request(url + '?parent_id=' + id, {
      asynchronous:true,
      evalScripts:false,
      onSuccess:processFolderJavascript
    });
  }
  return false;
}
  
var processFolderJavascript = function(t) {
  eval(t.responseText);
}

  
function addIdToHash(id) {
  if (hash.indexOf(id) == -1) {
    hash[hash.length] = id;
    updateHash();
  }
}

function removeIdFromHash(id) {
  for (i=0; i < hash.length; i++) {
    if ((hash[i] == id) || (hash[i].indexOf(id + '_') != -1)) {
      hash.splice(i,1);
      i=i-1;
    }
  }
  updateHash();
}

function updateHash() {
  hash_loc = window.location.href.indexOf('#')
  base_href = hash_loc == -1 ? window.location.href : base_href = window.location.href.substring(0,hash_loc);

  // why does opera do a page reload here instead of nicely just accepting the new URL
  if (window.navigator.userAgent.indexOf("Opera") >= 0) return;
  window.location.replace(base_href + '#' + hash.join('|'));
}
  
function updateAlternatingRows() {
  table = $('svnbrowsertable');
  children = table.getElementsByTagName('tr');
    
  for (i=0; i < children.length; i++) {
    if (i%2 == 1) {
      if (Element.hasClassName(children[i], 'alternate')) Element.removeClassName(children[i], 'alternate');
    } else {
      if (!Element.hasClassName(children[i], 'alternate')) Element.addClassName(children[i], 'alternate');
    }
  }
}
  
function finishLoadingNewRows(id) {
  updateAlternatingRows();
  Element.removeClassName($('href' + id), 'svnspinnerlink');
  Element.addClassName($('href' + id), 'svnfolderopenlink');
}
  
function createNewTableRow(url_base, cur_rev, insertafter, id, depth, linktext, created_rev, path, isdir, size, date, log) {
  tr = document.createElement('tr');
  tr.id = 'tr' + id;


  td = document.createElement('td');
  td.style.paddingLeft = '' + (depth+1)*16 + 'px';
  //td.innerHTML = '<input type="radio" name="radio" />'
  tr.appendChild(td);

  link = document.createElement('a');
  link.id = 'href' + id;
  link.innerHTML = linktext;
  if (isdir) {
    link.href = url_base + '/!dirinfo/' + cur_rev + '/' + path;
    link.onclick = function(e) {return clickedFolder(id, url_base + '/!tree/' + cur_rev + '/' + path);}
  } else {
    link.href = url_base + '/!source/' + cur_rev + '/' + path;
  }
    
  Element.addClassName(link, isdir ? 'svnfolderlink' : 'svnfilelink');
  td.appendChild(link);

  td = document.createElement('td');
  td.innerHTML = size;
  tr.appendChild(td);

  td = document.createElement('td');
  td.innerHTML = '<a href="' + url_base + '/!revision/' + created_rev + '">' + created_rev + '</a>';
  tr.appendChild(td);

  td = document.createElement('td');
  td.innerHTML = date;
  tr.appendChild(td);

  td = document.createElement('td');
  td.innerHTML = log;
  tr.appendChild(td);

  insertafter.parentNode.insertBefore(tr, insertafter.nextSibling);

  fake_click_link = false;
  // do we need to load it maybe?
  for (j=0; j < need_to_hash.length; j++) {
    if ((link.id == 'href' + need_to_hash[j]) && Element.hasClassName('href' + need_to_hash[j], "svnfolderlink")) {
      need_to_hash.splice(j,1);
      fake_click_link = true;
      break;
    }
  }

  if (load_path[0] == linktext) {
    load_path.splice(0,1);
    fake_click_link = true;
  }

  if (fake_click_link) {
    link.onclick(null);
  }

  return tr;
}

function updateFormFields() {
  var action = $('action').options[$('action').selectedIndex].value;
    
  if (action == "tree") {
    Element.show('for_revision');
  } else {
    Element.hide('for_revision');
  }

  if ((action == "pathdiff") || (action == "blame") || (action == "history") || (action == "source")) {
    Element.show('src1');
  } else {
    Element.hide('src1');
  }

  if (action == "pathdiff") {
    Element.show('src2');
  } else {
    Element.hide('src2');
  }
    
  if ($('prev_diff')) {
   if (action == "revdiff") {
     Element.show('prev_diff');
   } else {
     Element.hide('prev_diff');
   }
  }

  if (action == "history") {
    Element.show('revstop');
  } else {
    Element.hide('revstop');
  }
}
